
package com.cse210.grocerwe.server;

import org.restlet.resource.ServerResource;
import com.cse210.grocerwe.common.*;
import com.cse210.grocerwe.client.*;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import org.restlet.resource.Put;

public class APIResourceH extends ServerResource implements
APIInterfaceH
{
	PersistenceManager pm;
    @Put
    public void releaseList(String groupKey, String user)
    {
    	String[] parameters = groupKey.split("~");
    	user = parameters[0];
    	groupKey = parameters[1];
    	
    	pm = PMF.get().getPersistenceManager();
    	Query q = pm.newQuery(ShoppingItem.class);
		q.setFilter("group == x && status == 1");
		q.declareParameters("String x");
		@SuppressWarnings("unchecked")
	    List<ShoppingItem> items = (List<ShoppingItem>) q.execute(groupKey);
	    try {

	    	for( ShoppingItem item : items)
	    	{
	    		ShoppingItem detach = pm.detachCopy(item);
	    		detach.unlock(user);
	    		pm.makePersistent(detach);
	    	}
        } finally {
            pm.close();
        }
    }
}
